package me.caofeng.controller;

import com.sun.org.apache.regexp.internal.RE;
import me.caofeng.annotation.Authentication;
import me.caofeng.enums.ResponseCode;
import me.caofeng.pojo.Address;
import me.caofeng.pojo.Response;
import me.caofeng.pojo.User;
import me.caofeng.service.api.IAddressService;
import me.caofeng.service.api.IDataBindService;
import me.caofeng.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.controller
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-27 15:16
 * @Version: V1.0
 */
@RequestMapping(value = "address")
@Controller
@Authentication
public class AddressController {

    @Autowired
    private IDataBindService dataBindService;

    @Autowired
    private IAddressService addressService;

    private static Logger LOGGER = LogManager.getLogger(AddressController.class);

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Response<Address> addAddress(@Valid Address address, BindingResult result, HttpSession session) {
        Response response = dataBindService.verifyData(result);

        if (!response.isSuccess()) {
            return response;
        }

        User user = (User) session.getAttribute(Constant.USER);
        address.setUserId(user.getUid());
        return addressService.addAddress(address);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Response deleteAddress(@PathVariable("id") Integer id, HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return addressService.deleteAddress(id, user.getUid());
    }

    @RequestMapping(value = "{addressId}", method = RequestMethod.POST)
    @ResponseBody
    public Response<Address> updateAddress(
            @PathVariable("addressId") Integer addressId, @Valid Address address,
            BindingResult result, HttpSession session) {
        Response response = dataBindService.verifyData(result);

        if (!response.isSuccess()) {
            return response;
        }

        User user = (User) session.getAttribute(Constant.USER);
        return addressService.updateAddress(addressId, user.getUid(), address);
    }

    @RequestMapping(value = "{id}" ,method = RequestMethod.GET)
    @ResponseBody
    public Response<Address> getAddress(@PathVariable("id") Integer id,HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        return addressService.getAddress(id,user.getUid());
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Response<List<Address>> getAddresses(HttpSession session){
        User user = (User) session.getAttribute(Constant.USER);
        return addressService.getAddresses(user.getUid()) ;
    }

}
